// 中序和前序
let frontArr = ['A', 'B', 'D', 'E', 'C', 'F', 'G'];
let middleArr = ['D', 'B', 'E', 'A', 'F', 'C', 'G'];
let AfterArr = ['D', 'E', 'B', 'F', 'G', 'C', 'A'];

function Node(val) {
  this.value = val;
  this.left = null;
  this.right = null;
}
// 排序  中序+前序
function refresh(frontList, middleList) {
  if (frontList == null || middleList == null || frontList.length == 0 || middleList.length == 0 || middleList.length != frontList.length) return;
  let root = new Node(frontList[0]);
  let index = middleList.indexOf(root.value);

  let middleLeft = middleList.slice(0, index);
  let middleRight = middleList.slice(index + 1, middleList.length);

  let frontLLeft = frontList.slice(1, middleLeft.length + 1);
  let frontLRight = frontList.slice(middleLeft.length + 1, frontList.length);

  root.left = refresh(frontLLeft, middleLeft)
  root.right = refresh(middleRight, frontLRight);
  return root;
}

// 排序2 中序+后序
function refresh(AfterList, middleList) {
  if (AfterList == null || middleList == null || AfterList.length == 0 || middleList.length == 0 || middleList.length != AfterList.length) return;
  let root = new Node(AfterList[AfterList.length - 1]);
  let index = middleList.indexOf(root.value);

  let middleLeft = middleList.slice(0, index);
  let middleRight = middleList.slice(index + 1, middleList.length);

  let frontLLeft = AfterList.slice(0, middleLeft.length);
  let frontLRight = AfterList.slice(middleLeft.length, AfterList.length - 1);

  root.left = refresh(frontLLeft, middleLeft)
  root.right = refresh(middleRight, frontLRight);
  return root;
}






function flowFirst(root) {
  if (root == null) return;
  console.log(root.value);
  flowFirst(root.left)
  flowFirst(root.right)

}

// 排序1
flowFirst(refresh(frontArr, middleArr))
// 排序2
flowFirst(refresh(AfterArr, middleArr))